var express = require('express');
var router = express.Router();
var {listModel}=require('../model/model')
/* GET home page. */
// router.get('/', function(req, res, next) {
//   res.render('index', { title: 'Express' });
// });
router.post('/addList',async(req,res)=>{
  listModel.create(req.body)
  res.send({
    code:200,
    msg:'添加成功'
  })
})

router.get('/getList',async(req,res)=>{
  let {page,pageSize}=req.query
  let data=await listModel.find().lean().skip((page-1)*pageSize).limit(pageSize)
  let total=await listModel.find().count()
  data.forEach((item)=>{
    item.key = item._id
  })
  res.send({
    code:200,
    msg:'获取成功',
    data,
    total
  })
})

router.put('/updateList/:id',async(req,res)=>{
  let {id}=req.params
  await listModel.updateOne({_id:id},req.body)
  res.send({
    code:200,
    msg:'修改成功'
  })
})

router.delete('/deleteList/:id',async(req,res)=>{
  let {id}=req.params
  await listModel.deleteOne({_id:id})
  res.send({
    code:200,
    msg:'删除成功'
  })
  })

module.exports = router;
